package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Hycy;
import cn.stylefeng.guns.sys.modular.system.mapper.HycyMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.HycyParam;
import cn.stylefeng.guns.sys.modular.system.model.result.HycyResult;
import cn.stylefeng.guns.sys.modular.system.service.HycyService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-10-28
 */
@Service
public class HycyServiceImpl extends ServiceImpl<HycyMapper, Hycy> implements HycyService {

    @Override
    public void add(HycyParam param){
        Hycy entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(HycyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(HycyParam param){
        Hycy oldEntity = getOldEntity(param);
        Hycy newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HycyResult findBySpec(HycyParam param){
        return null;
    }

    @Override
    public List<HycyResult> findListBySpec(HycyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(HycyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }


    /**
     * 导入excel
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int batchImport(String fileName, MultipartFile file,String dataTime) throws Exception {


        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        //excel中多个sheet表，获取所有sheet表的数量
        int sheets = wb.getNumberOfSheets();

        List<Hycy> hycyList=new ArrayList<>();
        for (int i = 0; i < sheets; i++) {
            Sheet sheet = wb.getSheetAt(i);
            int lastRowNum = sheet.getLastRowNum() - 1;
            for (int r = 1; r <= lastRowNum; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //获取单元格
                Cell cell = row.getCell(0);
                if (ToolUtil.isNotEmpty(cell)) {
                    cell.setCellType(CellType.STRING);
                }
                //String date = row.getCell(15) == null ? "" : row.getCell(15).getStringCellValue().replaceAll("年", "-").replaceAll("月", "-").replaceAll("日", "");
                DataFormatter dataFormatter = new DataFormatter();
                String name = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();
                BigDecimal xj = row.getCell(2) == null ? BigDecimal.ZERO : new BigDecimal(row.getCell(2).getNumericCellValue());
                BigDecimal zfb = row.getCell(3) == null ? BigDecimal.ZERO : new BigDecimal(row.getCell(3).getNumericCellValue());
                BigDecimal wx = row.getCell(4) == null ? BigDecimal.ZERO : new BigDecimal(row.getCell(4).getNumericCellValue());
                BigDecimal md = row.getCell(14) == null ? BigDecimal.ZERO: new BigDecimal(row.getCell(14).getNumericCellValue());

                //插入数据
                Hycy hycy = new Hycy();

                //店名
                hycy.setMc(name);
                //现金
                hycy.setHyxj(xj);
                //支付宝
                hycy.setHyzfb(zfb);
                //微信
                hycy.setHywx(wx);
                //点评买单
                hycy.setHymd(md);
                hycy.setDateTime(dataTime);
                hycyList.add(hycy);

            }
        }

        if(CollectionUtil.isNotEmpty(hycyList)){
            for( Hycy hycy:hycyList){
                this.baseMapper.updateByNameAndDate(hycy);

            }
        }


        return 1;
    }

    private Serializable getKey(HycyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Hycy getOldEntity(HycyParam param) {
        return this.getById(getKey(param));
    }

    private Hycy getEntity(HycyParam param) {
        Hycy entity = new Hycy();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
